/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fss.client.ui;

import fss.transfer.TransferId;
import fss.transfer.TransferType;

/**
 *
 * @author hieu
 */
public class FileListItem
{
  private TransferId mTransferId;
  private boolean mIsComplete;
  private String mFileName;
  private long mFileSize;
  private long mCompleteByteSize;

  public static FileListItem createCompleteFile(String aFileName, long aFileSize)
  {
    return new FileListItem(true, aFileName, aFileSize);
  }

  public static FileListItem createIncompleteFile (String aFileName, long aFileSize, long aCompleteByteSize, TransferId aId)
  {
    return new FileListItem(false, aFileName, aFileSize, aCompleteByteSize, aId);
  }

  private FileListItem(boolean aIsComplete,
                       String aFileName,
                       long aFileSize)
  {
    mFileName = aFileName;
    mFileSize = aFileSize;
    mCompleteByteSize = aFileSize;
    mTransferId = null;
    mIsComplete = aIsComplete;
  }

  private FileListItem(boolean aIsComplete,
                       String aFileName,
                       long aFileSize,
                       long aIncompleteByteSize,
                       TransferId aId)
  {
    mFileName = aFileName;
    mFileSize = aFileSize;
    mCompleteByteSize = aIncompleteByteSize;
    mTransferId = aId;
    mIsComplete = aIsComplete;
  }

  public String getFileName()
  {
    return mFileName;
  }

  public long getFileSize()
  {
    return mFileSize;
  }

  public TransferId getIncompleteTransferId()
  {
    return mTransferId;
  }

  public boolean isIncompleteFile()
  {
    return (!mIsComplete);
  }

  public boolean isCompleteFile()
  {
    return mIsComplete;
  }

  public String getProgress()
  {
    String tReturn = "100%";
    if (isIncompleteFile())
    {
      double progress = ((double) mCompleteByteSize) / mCompleteByteSize;
      int percentage = (int) (progress * 100);
      tReturn = "" + percentage + "%";
    }
    return tReturn;
  }

  @Override
  public String toString()
  {
    StringBuilder tReturn = new StringBuilder();
    if (!mIsComplete)
    {
      String tTransferType = "Incomplete Transfer: ";
      if (mTransferId.getType() == TransferType.DOWNLOAD)
      {
        tTransferType = "Incomplete Download: ";
      }
      else if (mTransferId.getType() == TransferType.UPLOAD)
      {
        tTransferType = "Incomplete Upload: ";
      }
      tReturn.append(tTransferType)
        .append(mFileName);
    }
    else
    {
      tReturn.append(mFileName).append(" (").append(mFileSize).append(" bytes)");
    }
    return tReturn.toString();
  }
}
